//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdlib.h>

#include <iostream>
#include <fstream>

#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE 100000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main(int argc, char** argv) {

  String* filenameMatrix;
  String* filenameList;
  String* filenameOut;

  if (argc < 4) {
    cout << "Error in the input arguments. The execution must be: " << endl;
    cout << "./AddHeaderToMatrixFromList <Path of Matrix File> <Path of File with Ordered List> <Output Ordered Matrix File>" << endl;
    return 0;
  }
  else {
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "            Path of Matrix File : " << argv[1] << endl;
    cout << " Path of File with Ordered List : " << argv[2] << endl;
    cout << "     Output Ordered Matrix File : " << argv[3] << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    filenameMatrix = new String(argv[1]);
    filenameList   = new String(argv[2]);
    filenameOut    = new String(argv[3]);
  }

  fstream          fileMatrix, fileList, fileOut;

  TListE <String>* listHeaders = new TListE <String> ();

  char             buffer[LENGTHLINE + 1];

  fileList.open(filenameList->Out(), fstream::in);
  if (fileList.is_open()) {

    while (!fileList.eof()) {
      fileList.getline(buffer, LENGTHLINE);
      String* line = new String(buffer);
      line->Trim();
      if (line->Length()) listHeaders->Add(line);
      else                delete line;
    }
    fileList.close();

    fileOut.open(filenameOut->Out(), fstream::out);
    if (fileOut.is_open()) {
        
      listHeaders->SetInitial();
      while (listHeaders->SetNext()) {
        fileOut.width(8);
        fileOut.left;
        fileOut << listHeaders->GetCurrent()->Out() << " ";
      }
      fileOut << endl;

      fileMatrix.open(filenameMatrix->Out(), fstream::in);
      if (fileMatrix.is_open()) {
        while (!fileMatrix.eof()) {
          fileMatrix.getline(buffer, LENGTHLINE);
          fileOut << buffer << endl;
        }
        fileMatrix.close();
      }

      fileOut.close();
    }
    
  }

  cout << "---------------------------------------------------------------------------------" << endl;

  if (listHeaders)    delete listHeaders;

  if (filenameMatrix) delete filenameMatrix;
  if (filenameList)   delete filenameList;
  if (filenameOut)    delete filenameOut;

  return 1;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//


